package com.haoran.dwz.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import com.haoran.dwz.rest.R;
import com.haoran.dwz.service.DwzService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

@Api(description = "短网址模块", tags = "DWZ")
@RestController
public class DwzController {
	@Autowired
	private DwzService dwzService;

	@ApiOperation("生成短网址")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "form", name = "url", dataType = "string", required = true, value = "目标URL"),
			@ApiImplicitParam(paramType = "form", name = "type", dataType = "int", required = true, value = "短网址类型", defaultValue = "5") })
	@PostMapping("generate")
	@ApiResponses({
			// @ApiResponse(code=201,message="请求成功并且服务器创建了新的资源"),
			@ApiResponse(code = 400, message = "请求参数没有填好"), @ApiResponse(code = 401, message = "请求要求身份验证"),
			@ApiResponse(code = 404, message = "请求路径资源没有或者页面跳转路径不对"), @ApiResponse(code = 403, message = "您无权访问此资源") })
	public ResponseEntity<R> generate(String url, int type) {
		return  dwzService.generate(url, type);
	}

	/**
	 * 1位 [0,63] 
	 * 2位 [64,4095] 
	 * 3位 [4096,262143] 
	 * 4位 [262144,16777215] 
	 * 5位 [16777216,1073741823]
	 * 
	 * @param url
	 * @param type
	 * @return
	 */
	@ApiOperation("生成短网址测试")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "form", name = "type", dataType = "int", required = true, value = "短网址类型", defaultValue = "5") })
	@PostMapping("test-generate")
	@ApiResponses({
			// @ApiResponse(code=201,message="请求成功并且服务器创建了新的资源"),
			@ApiResponse(code = 400, message = "请求参数没有填好"), @ApiResponse(code = 401, message = "请求要求身份验证"),
			@ApiResponse(code = 404, message = "请求路径资源没有或者页面跳转路径不对"), @ApiResponse(code = 403, message = "您无权访问此资源") })
	public ResponseEntity<String> generate(int type) {

		if(type == 5) {
			for (int i = 16777216; i < 16777286; i++) {
				String s = "http://blog.csdn.net/who_is_xiaoming/article/details/";
				dwzService.generate(s + i, type);
			}
		}else if(type == 4) {
			for (int i = 262144; i < 262204; i++) {
				String s = "http://blog.csdn.net/who_is_xiaoming/article/details/";
				dwzService.generate(s + i, type);
			}
		}else if(type == 3) {
			for (int i = 4096; i < 4176; i++) {
				String s = "http://blog.csdn.net/who_is_xiaoming/article/details/";
				dwzService.generate(s + i, type);
			}
		}else if(type == 2) {
			for (int i = 64; i < 128; i++) {
				String s = "http://blog.csdn.net/who_is_xiaoming/article/details/";
				dwzService.generate(s + i, type);
			}
		}else if(type == 1) {
			for (int i = 0; i < 64; i++) {
				String s = "http://blog.csdn.net/who_is_xiaoming/article/details/";
				dwzService.generate(s + i, type);
			}
		}else {
			return ResponseEntity.badRequest().body("无效参数!");
		}
		return ResponseEntity.ok("成功!");
	}
}
